package com.zyj.classic;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class CountDownLatchTest {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(5); //初始化的时候, 设置 getCount() = 5
        for (int i = 1; i <= 100; i++) {
            final int fi = i;
            new Thread(() -> {
                try {
                    TimeUnit.SECONDS.sleep(fi);
                    System.out.println("count down " + fi);
                    countDownLatch.countDown(); //执行一次 getCount()会减1
                } catch (InterruptedException e) {
                }
            }).start();
        }
        //await()会阻塞, 直到 countDownLatch 的getCount() = 0
        countDownLatch.await(); //当countDownLatch.getCount()==0的时候, 会自动释放await的锁
        System.out.println("pass countDownLatch");
    }
}
